package entity;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class Test5 {
    public static void main(String[] args) {
        Properties properties = new Properties();//操作properti文件
        //获取配置文件输入流
        ClassLoader classLoader = Animal.class.getClassLoader();

        InputStream inputStream = classLoader.getResourceAsStream("reflect.properties");
        try {
            properties.load(inputStream);//加载配置文件信息
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据key只获取配置文件中的值
        String classname = properties.getProperty("classname");//entity。Animal
        System.out.println("classname = " + classname);
        String methondname = properties.getProperty("methondname");
        System.out.println("methondname = " + methondname);

        try {
            //利用反射获取Class对象
            Class<?> aClass = Class.forName(classname);
            //根据Class对象获取对应的方法
            Method declaredMethod = aClass.getDeclaredMethod(methondname);
            //执行方法
            declaredMethod.setAccessible(true);//取消安全检查
            Object o = aClass.newInstance();
            declaredMethod.invoke(o);//Method.invoke(对象)
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
